package factorytest.iflytek.com.einkiflytekfactorytest.engineer_test;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.viewpagerindicator.TabPageIndicator;

import java.util.List;

import factorytest.iflytek.com.einkiflytekfactorytest.R;
import factorytest.iflytek.com.einkiflytekfactorytest.utils.DebugLog;


public class EngineerActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_engineer);

        View txtReturn = findViewById(R.id.txtReturn);
        if (txtReturn != null) {
            txtReturn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setResult(RESULT_CANCELED);
                    finish();
                }
            });
        }

        FragmentPagerAdapter adapter = new EngineerTestPagerAdapter(getSupportFragmentManager());

        ViewPager pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(adapter);

        TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator);
        indicator.setViewPager(pager);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        DebugLog.e("engineer destroy");
    }

    private final class EngineerTestPagerAdapter extends FragmentPagerAdapter {
        public EngineerTestPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Class<? extends Fragment> cls = EngineerFragmentItemsEnum.values()[position].getFragmentClass();
            try {
                return cls.newInstance();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return TestFragment.newInstance("Error cls = " + cls);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return EngineerFragmentItemsEnum.values()[position].getName();
        }

        @Override
        public int getCount() {
            return EngineerFragmentItemsEnum.values().length;
        }
    }



}
